package cn.itsource.controller;

import cn.itsource.bean.Employee;
import cn.itsource.bean.Shop;
import cn.itsource.service.IEmployeeService;
import cn.itsource.service.IShopService;
import cn.itsource.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static cn.itsource.util.PetHomeConstant.EMP_STATUS_ACTIVATION;
import static cn.itsource.util.PetHomeConstant.SHOP.SHOP_STATUS_ACTIVATION;
import static cn.itsource.util.RedisContants.SHOP_ACTIVE_EMAIL;

/**
 * @author : 张金铭
 * @description :
 * @create :2022-11-14 16:43:00
 */
@Slf4j
@RestController
@RequestMapping("/email")
public class EmailController {

    @Resource
    private IEmployeeService employeeService;

    @Resource
    private IShopService shopService;

    @Resource
    private JavaMailSender javaMailSender;

    @Resource
    private StringRedisTemplate stringredisTemplate;


//    @PostMapping("/sendMsg")
//    public JsonResult<String> sendMsg(@RequestBody Employee user){
//        //获取到邮箱号
//        String email =user.getEmail();
//        if (!StringUtils.isNotEmpty(email)){
//            return JsonResult.error("验证发送失败，请检查邮箱是否填写");
//        }
//        //生成随机六位位随机数
//        String code = ValidateCodeUtils.generateValidateCode(4).toString();
//
//        //将随机数拼接到url
//        String url = SHOP_EMAIL_URL + code;
//
//        //调用邮箱发送的工具类完成验证码的发送
//        EmailUtil.sendEmail(javaMailSender,url,email);
//        log.info("验证码为： {}",code);
//        //验证码存入session
////        session.setAttribute(phone,code);
//        //将验证码存入redis中,设置过期时间为5分钟  key pethome:shop:code: + 6位随机数  value  email
//        stringredisTemplate.opsForValue().set(SHOP_ACTIVE_EMAIL + code,email,5, TimeUnit.MINUTES);
//        return JsonResult.success("验证码发送成功，请注意接收");
//    }



    @GetMapping("/active/{code}")
    public JsonResult activing(@PathVariable("code") String code){
        //将拿到的code和缓存拼接并查询
        String key = SHOP_ACTIVE_EMAIL + code;

        //查询
        String shopId = stringredisTemplate.opsForValue().get(key);

        if (Strings.isBlank(shopId)){
            return JsonResult.error("地址错误或已过期");
        }
        if (shopId==null){
            return JsonResult.error("地址错误或已过期");
        }

        //有value，正常激活，将邮箱匹配的店铺状态，和店铺管理员更改为已激活

        //匹配shop
        Shop shop = shopService.getById(shopId);
        shop.setState(SHOP_STATUS_ACTIVATION);

        //获取管理员id
        Long adminId = shop.getAdminId();

        //匹配店员
        Employee employee = employeeService.getById(adminId);
        employee.setState(EMP_STATUS_ACTIVATION);
        //更新店员数据和店铺数据

        employeeService.updateById(employee);
        shopService.updateById(shop);
        return JsonResult.success("账号激活成功");
    }
}
